// pages/user/user_browse/user_browse.js
const common = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: null,
        userInfo: null,
        dataList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            type: options.type
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            dataList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList()
    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getList()
            }
        }).catch(function (e) { })
    },

    getList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let dataList = this.data.dataList
        let data
        if (this.data.type == 'manager') {
            data = {
                client: 2,
                store_id: this.data.userInfo.store_id,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.type == 'filiale') {
            data = {
                client: 2,
                filiale_id: this.data.userInfo.filiale_id,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.type == 'shop') {
            data = {
                client: 2,
                store_id: this.data.userInfo.store_id,
                limit: 10,
                page: this.data.nowPage
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Userbrowse/user_browse', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading();
            if (res.code == 204) {
                that.setData({
                    dataList: dataList,
                    isAll: true
                })
            } else {
                dataList = dataList.concat(res)
                that.setData({
                    dataList: dataList,
                    nowPage: that.data.nowPage + 1
                })
            }
        })
    },

    phoneCall: function (e) {
        if (e.currentTarget.dataset.phone) {
            wx.makePhoneCall({
                phoneNumber: e.currentTarget.dataset.phone
            })
        }
    },

    goDetail: function (e) {
        wx.navigateTo({
            url: '/pages/user/user_browseDetail/user_browseDetail?userId=' + e.currentTarget.dataset.id
        })
    }
})